package fx.componet;

import fx.protocol.Anchor;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class ImageViewWithHoverEvent extends ImageView implements Anchor {
    private Image defaultImage;
    private Image hoverImage;

    public ImageViewWithHoverEvent(Image defaultImage, Image hoverImage) {
        super(defaultImage);
        this.hoverImage = hoverImage;
        this.defaultImage = defaultImage;
        if (hoverImage != null) {
            hover();
        }
    }

    public ImageViewWithHoverEvent(Image defaultImage) {
        super(defaultImage);
    }

    public void hover() {
        if (hoverImage != null) {
            setOnMouseEntered(event -> {
                setImage(hoverImage);
                setCursor(Cursor.HAND);
            });
            setOnMouseExited(event -> setImage(defaultImage));
        }
    }

    @Override
    public Node anchorEle() {
        return this;
    }

    public void fitSize(double w, double h) {
        setFitWidth(w);
        setFitHeight(h);
    }
}
